﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;

using System.Drawing.Drawing2D;
using Village_Project.Core;


namespace Village_Project
{
    public partial class rightsinfo : System.Web.UI.Page
    {

        private RegisterLog _RL;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CID"] == null)
            {

                MessageBoxurl("กรุณากดค้นหา หรือลงทะเบียนเด็กก่อน", "findchild.aspx");

            }
            else
            {

                String ShowValue = (string)Session["CID"];
                cid_label.Text = ShowValue;

                _RL = new RegisterLog();


                show_Images();

                show_Images2();
                show_Images3();

                if (!IsPostBack)
                {
                    showsession();
                    _RL.radiomedicalcare(medicalcare);

                    _RL.radioyesno_1(yesno1);
                    _RL.radioyesno_2(yesno2);







                    _RL.loaddatarightsinfoshow(cid_label, MedicalIDcardl, Dateofissuel, Dateofexpl, PrimaryCareClinicl, CareClinicForwardl, NumberDisabilitieIDl, NumberDisabilitieIDdatel, NumberDisabilitieIDexpl);

                    _RL.loaddatadropdownDisabilityTypeshow(DisabilityTypel, cid_label);
                    _RL.loaddataradiomedicalcareshow(cid_label, medicalcarel);

                    _RL.loaddataradioDisabilityAllowanceshow(cid_label, yesno1l);
                    _RL.loaddataradioCouponStudyshow(cid_label, yesno2l);
                }
            }


        }



        protected void showsession()
        {
            String ShowValue = (string)Session["CID"];
            cid_label.Text = ShowValue;




        }

        private void MessageBoxurl(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }

        public void show_Images()
        {
            string sj = "";



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT Child_Pic FROM [Person] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                sj = reader2["Child_Pic"].ToString();
            }
            else
            {
                sj = "~/pic/profile/nopic.jpg";
            }
            conn.Close();
            reader2.Close();


            Image1.ImageUrl = (sj);
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            Panel2.Visible = true;
            _RL.loaddatarightsinfo(cid_label, MedicalIDcard, Dateofissue, Dateofexp, PrimaryCareClinic, CareClinicForward, NumberDisabilitieID, NumberDisabilitieIDdate, NumberDisabilitieIDexp);

            _RL.loaddatadropdownDisabilityType(DisabilityType);
            _RL.loaddataradiomedicalcare(cid_label, medicalcare);

            _RL.loaddataradioDisabilityAllowance(cid_label, yesno1);
            _RL.loaddataradioCouponStudy(cid_label, yesno2);
            _RL.loaddataDisabilityType(cid_label, DisabilityType);

            Panel1.Visible = false;
        }

        protected void submit(object sender, EventArgs e)
        {
            //ok
            _RL.insertregdatarightsinfo(cid_label, MedicalIDcard.Text, Dateofissue.Text, Dateofexp.Text, PrimaryCareClinic.Text, CareClinicForward.Text, NumberDisabilitieID.Text, NumberDisabilitieIDdate.Text, NumberDisabilitieIDexp.Text, medicalcare, yesno1, yesno2, DisabilityType);
            ImageUpload_3();
            ImageUpload_4();
            Response.Redirect("rightsinfo.aspx");
        }






        protected void ImageUpload_3()
        {

            // SqlConnection conn = new SqlConnection(strConnString);
            if (FileUpload1.HasFile)
            {
                //try
                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection conn = new SqlConnection(constr);








                //ไปใส่พาธไฟล์เอา

                string MapPath = ("~/pic/note/");
                string filename = (string)Session["CID"];
                FileUpload1.SaveAs(Server.MapPath(MapPath) + filename + Path.GetExtension(FileUpload1.FileName));
                //FileUploader.SaveAs(filename + ".Jpeg");
                // string SQL = "INSERT INTO [ImageDek] ([Iddek],[FilePath]) VALUES (@Iddek,@Filepath)";
                //เพิ่มไฟล์ไทป์ด้วยนะคะพี่รงคะ
                string Includepath = ((MapPath) + filename + Path.GetExtension(FileUpload1.FileName));
                Bitmap originalBMP = new Bitmap(FileUpload1.FileContent);
                // ใส่ขนาดที่นี่
                int newWidth = 200;
                int newHeight = 200;
                Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
                Graphics oGraphics = Graphics.FromImage(newBMP);
                oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
                newBMP.Save(Server.MapPath(MapPath + filename + Path.GetExtension(FileUpload1.FileName)));
                originalBMP.Dispose();
                newBMP.Dispose();
                oGraphics.Dispose();


                SqlConnection scon = new SqlConnection(constr);
                SqlCommand cmd22 = new SqlCommand("UPDATE [ExistingWelfare] set DisableCardIDPic=@FilePath where CID=@Iddek", scon);
                cmd22.Parameters.AddWithValue("@Iddek", Session["CID"]);
                cmd22.Parameters.AddWithValue("@FilePath", Includepath);



                scon.Open();
                cmd22.ExecuteNonQuery();
                scon.Close();


                //xxx คิวรีมาจาก db เอา
                //Image1.ImageUrl = Server.MapPath("xxx");




                //}
                //catch
                //{

                //}
            }
        }









        protected void ImageUpload_4()
        {

            // SqlConnection conn = new SqlConnection(strConnString);
            if (FileUpload5.HasFile)
            {
                //try
                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection conn = new SqlConnection(constr);








                //ไปใส่พาธไฟล์เอา

                string MapPath = ("~/pic/medicalcare/");
                string filename = (string)Session["CID"];
                FileUpload5.SaveAs(Server.MapPath(MapPath) + filename + Path.GetExtension(FileUpload5.FileName));
                //FileUploader.SaveAs(filename + ".Jpeg");
                // string SQL = "INSERT INTO [ImageDek] ([Iddek],[FilePath]) VALUES (@Iddek,@Filepath)";
                //เพิ่มไฟล์ไทป์ด้วยนะคะพี่รงคะ
                string Includepath = ((MapPath) + filename + Path.GetExtension(FileUpload5.FileName));
                Bitmap originalBMP = new Bitmap(FileUpload5.FileContent);
                // ใส่ขนาดที่นี่
                int newWidth = 200;
                int newHeight = 200;
                Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
                Graphics oGraphics = Graphics.FromImage(newBMP);
                oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
                newBMP.Save(Server.MapPath(MapPath + filename + Path.GetExtension(FileUpload5.FileName)));
                originalBMP.Dispose();
                newBMP.Dispose();
                oGraphics.Dispose();


                SqlConnection scon = new SqlConnection(constr);
                SqlCommand cmd22 = new SqlCommand("UPDATE [ExistingWelfare] set InsuranceCardPic=@FilePath where CID=@Iddek", scon);
                cmd22.Parameters.AddWithValue("@Iddek", Session["CID"]);
                cmd22.Parameters.AddWithValue("@FilePath", Includepath);



                scon.Open();
                cmd22.ExecuteNonQuery();
                scon.Close();


                //xxx คิวรีมาจาก db เอา
                //Image1.ImageUrl = Server.MapPath("xxx");




                //}
                //catch
                //{

                //}
            }
        }
        protected void uploadpic_Click(object sender, EventArgs e)
        {
            ImageUpload_3();


        }





        public void show_Images2()
        {
            string sj = "";



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT DisableCardIDPic FROM [ExistingWelfare] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                sj = reader2["DisableCardIDPic"].ToString();
            }
            else
            {
                sj = "~/pic/profile/nopic.jpg";
            }
            conn.Close();
            reader2.Close();


            Image5.ImageUrl = (sj);
        }



        public void show_Images3()
        {
            string sj = "";



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT InsuranceCardPic FROM [ExistingWelfare] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                sj = reader2["InsuranceCardPic"].ToString();
            }
            else
            {
                sj = "~/pic/profile/nopic.jpg";
            }
            conn.Close();
            reader2.Close();


            Image4.ImageUrl = (sj);
        }


      
    }
}